﻿@using Xunit;
@using HorizontalAlignment = Microsoft.FluentUI.AspNetCore.Components.HorizontalAlignment
@inherits TestContext
@code
{
    [Inject]
    private LibraryConfiguration LibraryConfiguration { get; set; } = new LibraryConfiguration();

    public FluentDialogServiceTests()
    {
        JSInterop.Mode = JSRuntimeMode.Loose;
        Services.AddSingleton(LibraryConfiguration);
        var dialogUtilsModule = JSInterop.SetupModule("./_content/Microsoft.FluentUI.AspNetCore.Components/Components/Dialog/FluentDialogProvider.razor.js");
        dialogUtilsModule.SetupModule("getActiveElement", _ => true);
    }

    [Fact]
    public async Task FluentDialogService_Default()
    {
        Services.AddFluentUIComponents();

        // Arrange
        var cut = Render(@<FluentDialogProvider />);
        var dialogService = Services.GetRequiredService<IDialogService>();

        // Act
        var dialog = await dialogService.ShowDialogAsync<TestDialogHeaderFooter>(new DialogParameters()
            {
                Height = "200px",
                Title = "Sample title",
                PreventDismissOnOverlayClick = true,
                PreventScroll = true,
            });

        // Assert
        cut.Verify();
    }

	[Fact]
	public async Task FluentDialogService_ChangeHeaderTypo()
	{
		Services.AddFluentUIComponents();

		// Arrange
		var cut = Render(@<FluentDialogProvider />);
		var dialogService = Services.GetRequiredService<IDialogService>();

		// Act
		var dialog = await dialogService.ShowDialogAsync<TestDialogHeaderFooter>(new DialogParameters()
		{
			Height = "200px",
			Title = "Sample title as H2",
			TitleTypo = Typography.H2,
			PreventDismissOnOverlayClick = true,
			PreventScroll = true,
		});

		// Assert
		cut.Verify();
	}

    [Fact]
    public async Task FluentDialogService_HeightUnset()
    {
        Services.AddFluentUIComponents();

        // Arrange
        var cut = Render(@<FluentDialogProvider />);
        var dialogService = Services.GetRequiredService<IDialogService>();

        // Act
        var dialog = await dialogService.ShowDialogAsync<TestDialogHeaderFooter>(new DialogParameters()
            {
                Title = "Sample title",
                PreventDismissOnOverlayClick = true,
                PreventScroll = true,
            });

        // Assert
        cut.Verify();
    }

    [Fact]
    public async Task FluentDialog_RenderFragment()
    {
        Services.AddFluentUIComponents();

        // Arrange
        var cut = Render(@<FluentDialogProvider />);
        var dialogService = Services.GetRequiredService<IDialogService>();

        // Act
        var dialog = await dialogService.ShowDialogAsync(@<div>
        This is from a render fragment
    </div>,
        new DialogParameters()
            {
                Title = "Render Fragment Example",
                PreventDismissOnOverlayClick = true,
                PreventScroll = true,
            });

        // Assert
        cut.Verify();
    }

    [Fact]
    public async Task FluentDialogService_UpdateDialogAsync()
    {
        Services.AddFluentUIComponents();

        // Arrange
        var cut = Render(@<FluentDialogProvider />);
        var dialogService = Services.GetRequiredService<IDialogService>();

        var myUser = new UpdateDialogUser()
            {
                Id = 1,
                Name = "Denis",
            };

        var parameters = new DialogParameters<UpdateDialogUser>()
            {
                Height = "200px",
                Title = "My title",
                PrimaryAction = "Yes",
                PrimaryActionEnabled = false,
                SecondaryAction = "No",
            };

        // Act
        var dialog = await dialogService.ShowDialogAsync<TestDialogHeaderFooter>(myUser, parameters);

        parameters.Title = "My new title";
        parameters.PrimaryAction = "Oh yes";
        parameters.Content = new() { Id = 2, Name = "Vincent" };

        await dialogService.UpdateDialogAsync(dialog.Id, parameters);

        // Assert
        Assert.Equal("My new title", cut.Find("h4").InnerHtml);
        Assert.Equal("Oh yes", cut.Find("fluent-button[appearance='accent']").InnerHtml);
    }

    [Fact]
    public async Task FluentDialogService_AlignmentInvalid()
    {
        Services.AddFluentUIComponents();

        // Arrange
        var cut = Render(@<FluentDialogProvider/>   );
        var dialogService = Services.GetRequiredService<IDialogService>();

        // Act/Assert
        var exception = await Assert.ThrowsAsync<ArgumentException>(async () =>
        {
            var dialog = await dialogService.ShowDialogAsync<TestDialogHeaderFooter>(new DialogParameters()
            {
                Alignment = HorizontalAlignment.Stretch, // Stretch is not supported
            });
        });

        Assert.Equal("Alignment HorizontalAlignment.Stretch is not supported for DialogParameters", exception.Message);
    }

    private class UpdateDialogUser
    {
        public int Id { get; set; } = 0;
        public string Name { get; set; } = string.Empty;
    }
}
